(function() {
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  this.MyMessageListingView = (function(_super) {

    __extends(MyMessageListingView, _super);

    function MyMessageListingView() {
      this.afterLoad = __bind(this.afterLoad, this);
      return MyMessageListingView.__super__.constructor.apply(this, arguments);
    }

    MyMessageListingView.prototype.initialize = function() {
      var _this = this;
      MyMessageListingView.__super__.initialize.apply(this, arguments);
      this.setElement("#main");
      this.model.bind("change", this.render);
      this.model.fetch();
      $.connection.hub.start();
      this.chat = $.connection.chat;
      this.chat.client.send = function(userId) {
        if (userId === _this.model.get("UserId")) {
          _this.model.set("Page", 1);
          return _this.model.fetch();
        }
      };
    };

    MyMessageListingView.prototype.render = function() {
      var _this = this;
      return this.GetTemplate(SiteMap.Messages.MessageListingEmptyTemplate, function(template) {
        $(_this.el).html(template(_this.model.toJSON()));
        _this.startLoading();
        return _this.renewScrolling();
      });
    };

    MyMessageListingView.prototype.events = {
      "click button.Back": "Back",
      "click button.AddMessage": "AddMessage"
    };

    MyMessageListingView.prototype.Back = function() {
      return this.Navigate(SiteMap.Messages.UserListing);
    };

    MyMessageListingView.prototype.AddMessage = function() {
      var data,
        _this = this;
      data = $(this.el).find("form.general").serialize();
      this.model.set("Page", 1);
      return $.ajax({
        type: "POST",
        url: SiteMap.Messages.AddMessage + "?recepientId=" + this.model.get("SenderId"),
        data: data,
        success: function(response) {
          _this.model.fetch();
          $.connection.hub.start().done(function() {
            return _this.chat.server.send(_this.model.get("SenderId"));
          });
        }
      });
    };

    MyMessageListingView.prototype.afterLoad = function(data) {
      var _this = this;
      this.GetTemplate(SiteMap.Messages.MessageListingTemplate, function(compiledTemplate) {
        $(_this.el).find("div.message-listing ul").append(compiledTemplate(data));
      });
    };

    return MyMessageListingView;

  })(ScrollPagingView);

}).call(this);
